var express = require('express')
var router = express.Router()
var db = require('../public/javascripts/db');
var async = require('async');

router.get('/', (req, res, next) => {
    console.log(req.query.id)
    var sqls = {
        'sql1': 'SELECT * FROM results WHERE medical_num = ?',
        'sql2': 'SELECT * FROM users WHERE id = ?',
        'sql3': 'SELECT * FROM images WHERE id = ?'
    }
    var tasks = ['sql1', 'sql2', 'sql3'];
    var id = req.query.id;
    var img_id;
    var result = {};
    async.eachSeries(tasks, (item, callback) => {
        console.log('item', item);
        console.log('sqls[tasks]', sqls[item]);
        console.log('id:', id)
        db.query(sqls[item], id, (err, res) => {
            console.log('res', res)
            if (item == 'sql1') {
                id = res[0].owner_id;
                img_id = res[0].img_id;
                result['id'] = res[0].id;
                result['isDr'] = res[0].isDr;
                result['vessel'] = res[0].vessel;
                result['mass_vessel'] = res[0].mass_vessel;
                result['odlocation'] = res[0].odlocation;
                result['medical_num'] = res[0].medical_num;
                result['diagnostic_opinion'] = res[0].diagnostic_opinion;
            }
            else if (item == 'sql2') {
                id = img_id;
                result['phone'] = res[0].phone;
                result['name'] = res[0].name;
                result['identify_num'] = res[0].identify_num;
                result['gender'] = res[0].gender;
                result['age'] = res[0].age;
                result['birth_place'] = res[0].birth_place;
                result['birth_date'] = res[0].birth_date;
                result['medical_history'] = res[0].medical_history;
            } else {
                result['url'] = res[0].location;
            }
            callback(err, res)
        })
    }, (err2, res2) => {
        console.log('err:', err2);
        res.send({status: 2, data: result},)
    })
})

module.exports = router;